
#include "ui_test.h"
#include "img_src_header.h"


#define LOG_LEVEL CONFIG_LOG_DEFAULT_LEVEL
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(ui_test);

static uint32_t counter;
static lv_timer_t *timer;
static lv_obj_t *count_label;
static lv_obj_t *root_cont;
static lv_obj_t *title_label;
static lv_obj_t *ble_icon;

static void lv_timer_cb1(struct _lv_timer_t * timer)
{
    int8_t sub_title;

    counter ++;
    lv_label_set_text_fmt(count_label, "SystemTick %d", counter);

    sub_title = (counter % 4);
    if (sub_title == 0)
    {
	    lv_label_set_text_fmt(title_label, "%s serise", CONFIG_SOC_SERIES);
    }
    else if (sub_title == 1)
    {
	    lv_label_set_text_fmt(title_label, "Nordic Semi");
    }
    else if (sub_title == 2)
    {
      	lv_label_set_text_fmt(title_label, "A bule Tech");  
    }
    else if (sub_title == 3)
    {
       	lv_label_set_text_fmt(title_label, "%s", __DATE__); 
    }

}

static void anim_x_cb(void * var, int32_t v)
{
    lv_obj_set_x(var, v);
}

static void anim_y_cb(void * var, int32_t v)
{
    lv_obj_set_y(var, v);
}


static void button_events(lv_event_t *event)
{
    lv_event_code_t code = lv_event_get_code(event);
    int32_t key_value = lv_indev_get_key(lv_indev_get_act());

    if (code != LV_EVENT_KEY) {
        return;
    }
    LOG_INF(": %d", key_value);

    lv_anim_t anime;
    lv_anim_init(&anime);
    lv_anim_set_var(&anime, root_cont);

    switch (key_value)
    {
        case LV_KEY_LEFT:

            lv_anim_set_values(&anime, lv_obj_get_x(root_cont), MAX(lv_obj_get_x(root_cont)-40 , 0));
            lv_anim_set_exec_cb(&anime, anim_x_cb);            
        break;
        
        case LV_KEY_RIGHT:

            lv_anim_set_values(&anime, lv_obj_get_x(root_cont), MIN(lv_obj_get_x(root_cont)+40, DISPLAY_WIDTH - ROOT_COUNT_WIDTH));
            lv_anim_set_exec_cb(&anime, anim_x_cb);
        break;
        
        case LV_KEY_UP:

            lv_anim_set_values(&anime, lv_obj_get_y(root_cont), MAX(lv_obj_get_y(root_cont)-40, 0));
            lv_anim_set_exec_cb(&anime, anim_y_cb);
        break;

        case LV_KEY_DOWN:
 
            lv_anim_set_values(&anime, lv_obj_get_y(root_cont), MIN(lv_obj_get_y(root_cont)+40, DISPLAY_HEIGHT - ROOT_COUNT_HEIGHT));
            lv_anim_set_exec_cb(&anime, anim_y_cb);
        break;

        default:
            break;
    }

    lv_anim_set_time(&anime, 400);
    lv_anim_set_path_cb(&anime, lv_anim_path_linear);
    lv_anim_start(&anime);
}


lv_group_t* lv_app_reset_event_group(void)
{
    lv_indev_wait_release(lv_indev_get_act());
    lv_group_t* group = lv_group_get_default();
    LV_ASSERT_NULL(group);
    lv_group_set_wrap(group, true);
    lv_group_remove_all_objs(group);
    lv_group_set_focus_cb(group, NULL);

    return group;
}


void ui_ble_show(uint8_t enable)
{
    if(ble_icon == NULL)
    {
        return ;
    }

    if(enable)
    {
        lv_obj_clear_flag(ble_icon , LV_OBJ_FLAG_HIDDEN);
    }
    else
    {   
        lv_obj_add_flag(ble_icon , LV_OBJ_FLAG_HIDDEN);
    }
}


static int ui_main_test1(void)
{
    lv_group_t *group = lv_app_reset_event_group();

    root_cont = lv_obj_create(lv_scr_act());
    lv_obj_remove_style_all(root_cont);
    lv_obj_clear_flag(root_cont , LV_OBJ_FLAG_SCROLLABLE);

    lv_obj_set_size(root_cont , ROOT_COUNT_WIDTH , ROOT_COUNT_HEIGHT);
    lv_obj_set_style_radius(root_cont , 5 , LV_PART_MAIN);
    lv_obj_set_style_pad_left(root_cont , 5 , LV_PART_MAIN);
    lv_obj_set_style_pad_right(root_cont , 5 , LV_PART_MAIN);

    lv_obj_set_style_bg_color(root_cont, lv_palette_lighten(LV_PALETTE_BLUE, 1) , LV_PART_MAIN);
    lv_obj_set_style_bg_opa(root_cont, LV_OPA_COVER, LV_PART_MAIN);

    lv_obj_set_style_text_font(root_cont, &lv_font_montserrat_16, LV_PART_MAIN);
    lv_obj_set_style_text_color(root_cont, lv_color_white() , LV_PART_MAIN);

    lv_obj_set_x(root_cont , 85);
    lv_obj_set_y(root_cont , 90);

    ble_icon = lv_img_create(root_cont);
    lv_obj_set_align(ble_icon , LV_ALIGN_RIGHT_MID );
    lv_img_set_src(ble_icon , &img_src_ble);
    lv_obj_set_x(ble_icon , -10);
    lv_obj_add_flag(ble_icon , LV_OBJ_FLAG_HIDDEN);

    
    title_label = lv_label_create(root_cont);
	lv_label_set_text_fmt(title_label, "%s serise",CONFIG_SOC_SERIES);
	lv_obj_align(title_label, LV_ALIGN_LEFT_MID, 0, 0);
    lv_obj_set_x(title_label , 10);

	count_label = lv_label_create(lv_scr_act());
	lv_obj_align(count_label, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_obj_set_y(count_label , -5);

    lv_group_add_obj(group, root_cont);
    lv_obj_add_event_cb(root_cont, button_events, LV_EVENT_ALL, NULL);
    lv_group_focus_obj(root_cont);

    timer = lv_timer_create(lv_timer_cb1, 1000, NULL);
    if(timer == NULL)
    {
        return 0;
    }

    return 1;
}

/******************************************************************/


static void lv_timer_cb2(struct _lv_timer_t * timer)
{
    static uint8_t next = 0;


    next = (next + 1) % 3;

    if(next == 0)
    {
        lv_img_set_src(ble_icon , &img_src_test_1);
    }
    else if(next == 1)
    {
        lv_img_set_src(ble_icon , &img_src_test_2);
    }
    else if(next == 2)
    {
        lv_img_set_src(ble_icon , &img_src_test_3); 
    }
}


static int ui_main_test2(void)
{
    ble_icon = lv_img_create(lv_scr_act());
    lv_obj_set_align(ble_icon , LV_ALIGN_CENTER );
    lv_img_set_src(ble_icon , &img_src_test_1);
    

    timer = lv_timer_create(lv_timer_cb2, 200, NULL);
    if(timer == NULL)
    {
        return 0;
    }

    return 1;
}



/*************************************************************/



static void lv_timer_cb3(struct _lv_timer_t * timer)
{
    static uint8_t next = 0;

    next = (next + 1) % LV_PALETTE_GREY;

    lv_obj_set_style_bg_color(root_cont, lv_palette_lighten(next, 1) , LV_PART_MAIN); 
}


static int ui_main_test3(void)
{
    root_cont = lv_obj_create(lv_scr_act());
    lv_obj_remove_style_all(root_cont);
    lv_obj_clear_flag(root_cont , LV_OBJ_FLAG_SCROLLABLE);

    lv_obj_set_size(root_cont , DISPLAY_WIDTH , DISPLAY_HEIGHT);
    lv_obj_set_style_bg_color(root_cont, lv_palette_lighten(LV_PALETTE_BLUE, 1) , LV_PART_MAIN);
    lv_obj_set_style_bg_opa(root_cont, LV_OPA_COVER, LV_PART_MAIN);

    timer = lv_timer_create(lv_timer_cb3, 100, NULL);
    if(timer == NULL)
    {
        return 0;
    }

    return 1;
}


/*************************************************************/


int ui_main_test(void)
{
    return ui_main_test1();
    // return ui_main_test2();
    // return ui_main_test3();
}











